/*api 接口列表负责发起ajax请求*/
var mockDataService = {
    list: function (param) {
        return $.ajaxMock({
            contentType: 'application/json',
            type: 'POST',
            data: param,
            timeout: 5000,
            dataType: 'JSON'
        })
    }
}
$(function () {
    /*分页*/
    var tabledemo = shTable({
        templateId: 'table',
        rowKey: "userCode",
        data: [],
        cascade: true,
        childField: 'child',
        ajax: {
            method: mockDataService.list,
            /*参数构造器*/
            paramBuilder: function () {
                console.log("default paramBuilder!");
                return {};
            }
        },
        showConditions: {
            viewBtnShowConditions: [
                function (row) {
                    return row.getData().userCode=='lufei';
                },
                function (row) {
                    return row.getData().userName=='路飞';
                }
            ]
        },
        pagination: {
            /*是否ajax分页*/
            open: true,
            pageSize: 10
        },
        tableEvent: {
            view: function (tableObj, row) {
                console.log(JSON.stringify(row.getData()))
            }
        },
        initSuccess: function (obj) {
            $("#btn1").click(function () {
                console.log(JSON.stringify(obj.getCheckedKeys()));
            })
            $("#btn2").click(function () {
                console.log(JSON.stringify(obj.getCheckedRecursions()));
            })
            $("#btn3").click(function () {
                console.log(JSON.stringify(obj.getCheckedObject()));
            })
        }
    }).mount($("#tableEl"));
    /*不分页*/
    var tableNoPage = shTable({
        templateId: "tableNoPage",
        data: data,
        rowKey: "userCode",
        pagination: {
            /*是否ajax分页*/
            open: false,
            pageSize: 10
        },
        tableEvent: {
            view: function (tableObj, row) {
                console.log(JSON.stringify(row.getData()))
            }
        }
    }).mount();
    var dataDemo = shTable({
        templateId: "dataDemo",
        rowKey: "userCode",
        filters: {
            reverse: function (v) {
                return v.split("").reverse().join("");
            }
        },
        pagination: {
            /*是否ajax分页*/
            open: true,
            pageSize: 10
        },
        data: data,
        tableEvent: {
            view: function (tableObj, row) {
                alert(JSON.stringify(row.getData()))
            }
        }
    }).mount();
    var hbasedemo = shTable({
        templateId: "hbasetable", rowKey: "userCode",
        data: [],
        ajax: {
            method: mockDataService.list,
            /*参数构造器*/
            paramBuilder: function () {
                console.log("default paramBuilder!");
                return {};
            }
        },
        pagination: {
            /*是否ajax分页*/
            open: true,
            pageSize: 5,
            pageModel: "3"
        },
        tableEvent: {
            view: function (tableObj, row) {
                console.log(JSON.stringify(row.getData()))
            }
        }
    }).mount();
    var editTable = shTable({
        templateId: "editTable", rowKey: "userCode",
        filters: {
            reverse: function (v) {
                return v.split("").reverse().join("");
            }
        },
        pagination: {
            /*是否ajax分页*/
            open: true,
            pageSize: 10
        },
        data: data,
        tableEvent: {
            view: function (tableObj, row) {
                alert(JSON.stringify(row.getData()))
            }
        }
    }).mount();

    $("#editDataPrint").click(function () {
        console.log(JSON.stringify(editTable.getAllData()))
    });


    //js美化
    $('.beautify').each(function () {
        beautify(this);
    });

})


var the = {beautify_in_progress: false};
//this dummy function alleviates Chrome large string corruption by probably shoveling the corruption bug to some other area
if (/chrome/.test(navigator.userAgent.toLowerCase())) {
    String.prototype.old_charAt = String.prototype.charAt;
    String.prototype.charAt = function (n) {
        return this.old_charAt(n);
    }
}

function unpacker_filter(source) {
    var trailing_comments = '';
    var comment = '';
    var found = false;

    do {
        found = false;
        if (/^\s*\/\*/.test(source)) {
            found = true;
            comment = source.substr(0, source.indexOf('*/') + 2);
            source = source.substr(comment.length).replace(/^\s+/, '');
            trailing_comments += comment + "\n";
        } else if (/^\s*\/\//.test(source)) {
            found = true;
            comment = source.match(/^\s*\/\/.*/)[0];
            source = source.substr(comment.length).replace(/^\s+/, '');
            trailing_comments += comment + "\n";
        }
    } while (found);

    return trailing_comments + source;
}

function beautify(elem) {
    if (the.beautify_in_progress) return;

    the.beautify_in_progress = true;

    // var source = $('#source').val();
    var source = $(elem).html();

    var indent_size = $('#tabsize').val();
    var indent_char = indent_size == 1 ? '\t' : ' ';
    var preserve_newlines = $('#preserve-newlines').attr('checked');
    var keep_array_indentation = $('#keep-array-indentation').attr('checked');
    var brace_style = $('#brace-style').val();

    if ($('#detect-packers').attr('checked')) {
        source = unpacker_filter(source);
    }

    var comment_mark = '<-' + '-';
    var opts = {
        indent_size: indent_size,
        indent_char: indent_char,
        preserve_newlines: preserve_newlines,
        brace_style: brace_style,
        keep_array_indentation: keep_array_indentation,
        space_after_anon_function: true
    };

    if (source && source[0] === '<' && source.substring(0, 4) !== comment_mark) {
        $(elem).html(
            style_html(source, opts)
        );
    } else {
        var v = js_beautify(unpacker_filter(source), opts);
        $(elem).html(v);
    }

    the.beautify_in_progress = false;
}